package com.CmJava.aop;

import com.CmJava.annotation.aop.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Aspect
public class MyAspect {

    @Pointcut("execution(public com.CmJava.service.MyServiceImpl.*(..))")
    public void pointCut(){}


    @Before("pointCut()")
    public void doAccessCheck(Method method,Object bean,Object... args) throws InvocationTargetException, IllegalAccessException {
        System.out.println("前置通知");
        method.invoke(bean,args);
    }

    @AfterReturning("pointCut()")
    public void doAfter(Method method,Object bean,Object... args) throws InvocationTargetException, IllegalAccessException {

            method.invoke(bean,args);
            System.out.println("后置通知");

    }

    @After("pointCut()")
    public void after(Method method,Object bean,Object... args){
        try {
            method.invoke(bean,args);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("最终通知");
        }
    }

    @AfterThrowing("pointCut()")
    public void doAfterThrow(Method method,Object bean,Object... args){
        try {
            System.out.println("进入错误通知");
            method.invoke(bean,args);
            System.out.println("退出错误通知");
        }catch (Exception e){
            System.out.println("错误通知");
        }
    }

    @Around("pointCut()")
    public Object doAround(Method method,Object bean,Object... args) throws Throwable{
        System.out.println("进入环绕通知");
        //执行该方法
        method.invoke(bean,args);
        System.out.println("退出环绕通知");
        return bean;
    }


}
